package com.zsqat.eshop.product.service.impl;

import com.zsqat.eshop.product.vo.AttrGroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsqat.common.utils.PageUtils;
import com.zsqat.common.utils.Query;

import com.zsqat.eshop.product.dao.AttrAttrgroupRelationDao;
import com.zsqat.eshop.product.entity.AttrAttrgroupRelationEntity;
import com.zsqat.eshop.product.service.AttrAttrgroupRelationService;

import javax.annotation.Resource;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {
  @Resource
  private AttrAttrgroupRelationDao relationDao;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<AttrAttrgroupRelationEntity> page = this.page(
        new Query<AttrAttrgroupRelationEntity>().getPage(params),
        new QueryWrapper<AttrAttrgroupRelationEntity>()
    );

    return new PageUtils(page);
  }

  @Override
  public void saveBatch(List<AttrGroupRelationVo> attrGroupRelationVos) {
    List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrGroupRelationVos.stream()
        .map(x -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(x, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
    }).collect(Collectors.toList());
    this.saveBatch(attrAttrgroupRelationEntities);
  }

  // 移除分组和属性之间的关联关系
  @Override
  public void deleteRelation(AttrGroupRelationVo[] attrGroupRelationVos) {
    List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = Arrays.asList(attrGroupRelationVos)
        .stream()
        .map((x) -> {
          AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
          BeanUtils.copyProperties(x, attrAttrgroupRelationEntity);
          return attrAttrgroupRelationEntity;
    }).collect(Collectors.toList());

    relationDao.deleteBatchRelation(attrAttrgroupRelationEntities);
  }

}